package com.hgx.lc.core.facade;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 文件接口demo示例
 *
 * @author HGX
 * @since: 2020-12-16 13:03
 */
@Controller
public class FileDemoController {


    /**
     * 文件上传
     *
     * @param file 文件
     * @return 结果
     */
    @RequestMapping("fileUpload")
    @ResponseBody
    public String fileUpload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "false";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" + size);

        String path = "F:/test";
        File dest = new File(path + "/" + fileName);
        if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            return "true";
        } catch (IllegalStateException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        }
    }


    /**
     * 实现多文件上传
     *
     * @param request http请求
     * @return 返回
     */
    @RequestMapping(value = "multipleFilesUpload", method = RequestMethod.POST)
    @ResponseBody
    public String multipleFilesUpload(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("fileName");
        if (files.isEmpty()) {
            return "false";
        }

        String path = "F:/test";

        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            int size = (int) file.getSize();
            System.out.println(fileName + "-->" + size);

            if (file.isEmpty()) {
                return "false";
            } else {
                File absoluteFile = getFileUrl().getAbsoluteFile();
                //构建真实的文件路径
                File newFile = new File(absoluteFile + File.separator + file.getOriginalFilename());
                try {
                    file.transferTo(newFile);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return "false";
                }
            }
        }
        return "true";
    }


    public static File getFileUrl() {

        // 构建上传文件的存放 "文件夹" 路径
        String fileDirPath = "src/main/resources/static/files";

        File fileDir = new File(fileDirPath);
        if (!fileDir.exists()) {
            // 递归生成文件夹
            boolean mkdirs = fileDir.mkdirs();
        }
        return fileDir;

    }


}
